﻿using System;
using System.Linq;
using System.Windows.Forms;
using AMM.CustomWinControls;
using AMM.Forms.Reports;
using Microsoft.Reporting.WinForms;

namespace AMM.Forms {
    public partial class FrmReport : FrmRootBase {

        private FrmReportSearch _frmReportSearch;

        public string FunctionCode { get; set; }
        public BaseReportSearchControl ReportSearchControl { get; set; }

        private void Search(object sender, FrmReportSearch.SearchArgs e) {
            if (this.InvokeRequired) {
                this.Invoke(new ThreadDelegate<FrmReportSearch.SearchArgs>(this.RefreshReport), e);
            }
            else {
                this.RefreshReport(e);
            }
        }

        private void RefreshReport(FrmReportSearch.SearchArgs e) {
            this.rvViewer.LocalReport.ReportPath = string.Format(@"{0}\Reports\{1}", Application.StartupPath, e.ReportName);
            this.rvViewer.LocalReport.DataSources.Clear();
            this.rvViewer.LocalReport.DataSources.Add(new ReportDataSource("ds", e.Datasource));
            this.rvViewer.RefreshReport();           
        }

        private void BindExportItem() {
            this.rvViewer.LocalReport.ListRenderingExtensions().ToList().ForEach(x => {
                if (x.Visible) {
                    this.tsBtnExport.DropDownItems.Add(x.Name).Click += (s, e) => {
                        this.rvViewer.ExportDialog(x);
                    };
                }
            });
        }

        public FrmReport() {
            this.InitializeComponent();
        }

        private void FrmReportBase_Load(object sender, EventArgs e) {
            this.WindowState = FormWindowState.Maximized;
            this.rvViewer.RefreshReport();
            this.rvViewer.RenderingComplete += (a, b) => {
                this.pageList.Records = this.rvViewer.GetTotalPages();
                this.pageList.DataBind();
            };
            this.rvViewer.PageNavigation += (a, b) => {
                b.Cancel = true;
            };
            this.BindExportItem();
        }


        private void tsBar_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {

            Action actPrint = () => {
                this.rvViewer.PrintDialog();
            };

            Action actSearch = () => {
                if (this.ReportSearchControl == null || this.ReportSearchControl.IsDisposed) { return; }
                if (this._frmReportSearch == null || this._frmReportSearch.IsDisposed) {
                    this._frmReportSearch = new FrmReportSearch();
                    this._frmReportSearch.Controls.Add(this.ReportSearchControl);
                    this._frmReportSearch.Search += new FrmReportSearch.ReportSearchEventHandler(Search);
                }
                this._frmReportSearch.Text = "搜索";
                this._frmReportSearch.ShowDialog(this);
            };

            Action actExport = () => {
                // nothing need to be done, only show the dropdownitems
            };

            this.tsBar.Focus();
            switch (e.ClickedItem.Name) {
                case "tsBtnPrint": actPrint.Invoke(); break;
                case "tsBtnExport": actExport.Invoke(); break;
                case "tsBtnSearch": actSearch.Invoke(); break;
                case "tsBtnQuit": this.Close(); break;
            }
        }

        private void pageList_Paging(object sender, PaginationArgs e) {
            this.rvViewer.CurrentPage = e.PageIndex;
        }
    }
}
